<?php
namespace app\shop\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\shop\controller\Common;
use app\service\lifeapiService;//生活号接口
use app\service\shopService;
use printing\printShop;
use think\Log;
class Merchant extends Common{
	public function getshopinfo($user_id){
		return Db::table('zz_shop')->where('user_id',$user_id)->find();
	}

	//线下订单列表
	public function orderListOffline(){
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}
		$where["shop_id"]=$shopinfo["shop_id"];
		$orderlist=(new shopService())->shoporderlist($where);
		$this->assign("orderlist",$orderlist);
		return View::fetch('order_list_offline');
	}


	//线上订单列表
	public function orderListOnline(){
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}
		$where["shop_id"]=$shopinfo["shop_id"];
		$orderlist=Db::table("zz_order_online")->where('shop_id',$shopinfo["shop_id"])->where('order_status','1')->order('order_id desc')->select();

		$this->assign("orderlist",$orderlist);
		return View::fetch('order_list_online');
	}

	public function orderinfo(){
		
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}
		$order_id=request()->get("order_id");
		$orderinfo=Db::table("zz_order_online")->where("order_id",$order_id)->find();
		$ordergoods=Db::table("zz_order_goods")->where("order_id",$order_id)->select();
		$this->assign("orderinfo",$orderinfo);
		$this->assign("ordergoods",$ordergoods);
		$this->assign("title","订单详情");
		
		return View::fetch('order_info');
	}

	public function uploadImg(){
		$file = request()->file('file');
        if(!empty($file)){ 
            $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'shop'. DS . 'upload');
            if($info){
                $data['pic'] = $info->getSaveName(); 
                $data["state"]="1";
                echo json_encode($data);
            }else{
            	$data['msg'] =  $file->getError();; 
                $data["state"]="0";
                echo json_encode($data);
            }
           
        }
	}
	public function uploadBase64Img(){
		$file = request()->post();
		$logo_data = $data["picture"];
		$reg = '/data:image\/(\w+?);base64,(.+)$/si';
		preg_match($reg,$logo_data,$match_result);
    	$file_name = date('YmdHis').rand(100000,999999).'.'.$match_result[1];
    	$logo_path = 'C:\phpStudy\WWW\zaozao\public\shop\upload\\'.$file_name;
        $num = file_put_contents($logo_path,base64_decode($match_result[2]));
        if(!empty($num)){
        	$data['picture']=$file_name;
        	$data['pic'] = $file_name;; 
            $data["state"]="1";
            echo json_encode($data);
        }else{
        	$data['msg'] =  $file->getError();; 
            $data["state"]="0";
            echo json_encode($data);
        	$this->error('图片上传失败');
        }
	}

	//商品添加

	public function goodAdd(){
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}

		if(request()->isPost()){

			$data = request()->post();
            $file = request()->file('file');
	        if(!empty($data["picture"])){
            	$logo_data = $data["picture"];
        		$reg = '/data:image\/(\w+?);base64,(.+)$/si';
        		preg_match($reg,$logo_data,$match_result);
            	$file_name = date('YmdHis').rand(100000,999999).'.'.$match_result[1];
            	$logo_path = 'C:\phpStudy\WWW\zaozao\public\shop\upload\\'.$file_name;
                $num = file_put_contents($logo_path,base64_decode($match_result[2]));
                if(!empty($num)){
                	$data['picture']=$file_name;
                }else{
                	$this->error('图片上传失败');
                }
                
            }else{
            	$this->error('请选择要上传的图片');
            }


            $data["shop_id"]=$shopinfo["shop_id"];
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["salesprice"]=$data["price"];
            //echo "<pre>";
            //var_dump($data);die;
            $res=Db::table("zz_goods")->insert($data);
            if($res){
            	 $this->success('商品添加成功',url('goodlist'));
            }else{
            	 $this->error('商品添加失败');
            }
		}else{
			//查询分类列表
			$category=Db::table('zz_category')->order('sort desc')->select();
			$this->assign("category",$category);
			$this->assign('shopinfo',$shopinfo);
			return View::fetch('good_add');
		}	
	}

	public function goodAdd1(){
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}

		if(request()->isPost()){

			$data = request()->post();
            $file = request()->file('file');
	        if(!empty($data["picture"])){
            	$logo_data = $data["picture"];
        		$reg = '/data:image\/(\w+?);base64,(.+)$/si';
        		preg_match($reg,$logo_data,$match_result);
            	$file_name = date('YmdHis').rand(100000,999999).'.'.$match_result[1];
            	$logo_path = 'C:\phpStudy\WWW\zaozao\public\shop\upload\\'.$file_name;
                $num = file_put_contents($logo_path,base64_decode($match_result[2]));
                if(!empty($num)){
                	$data['picture']=$file_name;
                }else{
                	$this->error('图片上传失败');
                }
                
            }else{
            	$this->error('请选择要上传的图片');
            }


            $data["shop_id"]=$shopinfo["shop_id"];
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["salesprice"]=$data["price"];
            //echo "<pre>";
            //var_dump($data);die;
            $res=Db::table("zz_goods")->insert($data);
            if($res){
            	 $this->success('商品添加成功',url('goodlist'));
            }else{
            	 $this->error('商品添加失败');
            }
		}else{
			//查询分类列表
			$category=Db::table('zz_category')->order('sort desc')->select();
			$this->assign("category",$category);
			$this->assign('shopinfo',$shopinfo);
			return View::fetch('good_add');
		}	
	}

	//促销商品添加
	public function goodAddPro(){
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}
		if(request()->isPost()){
			$data = request()->post();
            $file = request()->file('file');
	        if(!empty($data["picture"])){
            	$logo_data = $data["picture"];
        		$reg = '/data:image\/(\w+?);base64,(.+)$/si';
        		preg_match($reg,$logo_data,$match_result);
            	$file_name = date('YmdHis').rand(100000,999999).'.'.$match_result[1];
            	$logo_path = 'C:\phpStudy\WWW\zaozao\public\shop\upload\\'.$file_name;
                $num = file_put_contents($logo_path,base64_decode($match_result[2]));
                if(!empty($num)){
                	$data['picture']=$file_name;
                }else{
                	$this->error('图片上传失败');
                }
                
            }else{
            	$this->error('请选择要上传的图片');
            }
            $data["shop_id"]=$shopinfo["shop_id"];
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["promotion_type"]="1";
            //echo "<pre>";
            //var_dump($data);die;
            $res=Db::table("zz_goods")->insert($data);
            if($res){
            	 $this->success('促销商品添加成功');
            }else{
            	 $this->error('促销商品添加失败');
            }
		}else{
			//查询分类列表
			$category=Db::table('zz_category')->order('sort desc')->select();
			$this->assign("category",$category);
			$this->assign('shopinfo',$shopinfo);
			return View::fetch('good_add_promo');
		}	
	}

	public function goodYzl(){
		
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return "您无权访问";
		}
		if(request()->isPost()){
			$data = request()->post();
            $file = request()->file('file');
	        if(!empty($file)){ 
	            $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'shop'. DS . 'upload');
	            if($info){
	                $data['picture'] = $info->getSaveName(); 
	               
	            }else{
	            	echo $file->getError();; 
	            	die;     
	            }
	        }else{
	        	$this->error('请选择要上传的图片');
	        }
            $data["shop_id"]=$shopinfo["shop_id"];
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["promotion_type"]="1";
            //echo "<pre>";
            //var_dump($data);die;
            $res=Db::table("zz_goods")->insert($data);
            if($res){
            	 $this->success('促销商品添加成功');
            }else{
            	 $this->error('促销商品添加失败');
            }
		}else{
			//查询分类列表
			$category=Db::table('zz_category')->order('sort desc')->select();
			$this->assign("category",$category);
			$this->assign('shopinfo',$shopinfo);
			return View::fetch('good_yzl');
		}	
	}
	//促销商品添加
	public function goodYzlPro(){
		session("user_id","2088112618452297");
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}
		if(request()->isPost()){
			$data = request()->post();
            if(!empty($data["picture"])){
            	$logo_data = $data["picture"];
        		$reg = '/data:image\/(\w+?);base64,(.+)$/si';
        		preg_match($reg,$logo_data,$match_result);
            	$file_name = date('YmdHis').rand(100000,999999).'.'.$match_result[1];
            	$logo_path = 'C:\phpStudy\WWW\zaozao\public\shop\upload\\'.$file_name;
                $num = file_put_contents($logo_path,base64_decode($match_result[2]));
                if(!empty($num)){
                	$data['picture']=$file_name;
                }else{
                	$this->error('图片上传失败');
                }
                
            }else{
            	$this->error('请选择要上传的图片');
            }
            $data["shop_id"]=$shopinfo["shop_id"];
            $data["shop_name"]=$shopinfo["shop_name"];
            $categoryinfo=Db::table("zz_category")->where("category_id",$data["category_id"])->find();
            $data["category_name"]= $categoryinfo["category_name"];
            $data["promotion_type"]="1";
            //echo "<pre>";
            //var_dump($data);die;
            $res=Db::table("zz_goods")->insert($data);
            if($res){
            	 $this->success('促销商品添加成功');
            }else{
            	 $this->error('促销商品添加失败');
            }
		}else{
			//查询分类列表
			$category=Db::table('zz_category')->order('sort desc')->select();
			$this->assign("category",$category);
			$this->assign('shopinfo',$shopinfo);
			return View::fetch('good_yzl_promo');
		}	
	}

	//商品列表
	public function goodlist(){
		//session("openid","2088112618452297");
		session("url",request()->url());
		$shopinfo=$this->getshopinfo(session('user_id'));
		if(empty($shopinfo)){
			return View::fetch('nopermission');
		}
		$goodlist=Db::table("zz_goods")->where("shop_id",$shopinfo["shop_id"])->order('goods_id desc')->select();
		$this->assign("goodlist",$goodlist);
		$this->assign("title","商品列表");
		return View::fetch('good_list');
	}

	public function goodDel(){
		$goods_id=request()->get("goods_id");
		$goodsinfo=Db::table("zz_goods")->where("goods_id",$goods_id)->find();
		if(empty($goodsinfo)){
			return View::fetch('nopermission');
		}
		$res=Db::table("zz_goods")->where("goods_id",$goods_id)->delete();
		if($res){
			Db::table("zz_shopcart")->where("goods_id",$goods_id)->delete();
			Db::table("zz_goods_delete")->insert($goodsinfo);
			$url=url('goodlist');
			echo "<script>alert('商品删除成功');location.href='$url'</script>";
			exit;
			$this->success("商品删除成功",url('goodlist'));
		}else{
			$this->error("商品删除失败");
		}	
	}

	public function goodUp(){
		$goods_id=request()->get("goods_id");
		$goodsinfo=Db::table("zz_goods")->where("goods_id",$goods_id)->find();
		if(empty($goodsinfo)){
			return View::fetch('nopermission');
		}
		$res=Db::table("zz_goods")->where("goods_id",$goods_id)->update(["state"=>"1"]);
		if($res){
			$url=url('goodlist');
			echo "<script>alert('商品上架成功');location.href='$url'</script>";
			exit;
			
		}else{
			$this->error("商品上架失败");
		}	
	}
	public function goodDown(){
		$goods_id=request()->get("goods_id");
		$goodsinfo=Db::table("zz_goods")->where("goods_id",$goods_id)->find();
		if(empty($goodsinfo)){
			return View::fetch('nopermission');
		}
		$res=Db::table("zz_goods")->where("goods_id",$goods_id)->update(["state"=>"3"]);
		if($res){
			Db::table("zz_shopcart")->where("goods_id",$goods_id)->delete();
			$url=url('goodlist');
			echo "<script>alert('商品下架成功');location.href='$url'</script>";
			exit;
			
		}else{
			$this->error("商品下架失败");
		}	
	}
}